<?php

/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
    exit('Access Denied');
}

require_once(TIN_APP_PATH . 'include/TinAppController.php');
require_once(TIN_APP_PATH . "include/utils/GeoCodeUtil.php");
require_once(TIN_APP_PATH . 'include/TinPHP/TinMemcache.php');

/** 地区信息管理 */
class AjaxGeoController extends TinAppController {
	
	private $geoLogic;
	
    public function __construct() {
        parent::__construct();
        T::$tplFile = "public/empty";
        $this->geoLogic = new GeoLogic(); //$this->Logic("GeoLogic");
    }
    
    public function actionGee(){
    	$res = $this->geoLogic->getGeoChildAll("*", '68174594');
    	var_dump($res);
    }
    /** 根据GEO_CODE获取地区列表 */
    public function actionGetInitGeoTree() {
        $geoCode = ($_REQUEST["geo_code"]);
        $initLevel = intval($_REQUEST["start_level"]) ? intval($_REQUEST["start_level"]) : 1;
        $result = array();
        $cacheKey = "mall_geoinit" . $geoCode . $initLevel;

        $result = TinMemcache::get($cacheKey);
        if (!$result) {
            if ($geoCode == 0) {            	
            	$getList = $this->geoLogic->getGeoAll("*","status > 1 AND parentid = 0");            	
                $result[] = array("current" => "null",
                    "child" => $getList
                );
            } else {
                $geoArray = GeoCodeUtil::getGeoCodeArray($geoCode);
                $geoTree = array(0);
                foreach ($geoArray as $val)
                    $geoTree[] = $val;
                
                for ($i = $initLevel - 1; $i < count($geoTree) - 1; $i++) {
                    if ($i < $initLevel - 1)
                        continue;
                    $req1 = $this->buildGeoReq($geoTree[$i], "get_child", $i);
                    $res["get_child" . $i] = $req1;
                    
                    $req2 = $this->buildGeoReq($geoTree[$i + 1], "get_current", $i);
                    $res["get_current" . $i] = $req2;
                    
                    if ($i == count($geoTree) - 2) {
                        $req3 = $this->buildGeoReq($geoTree[$i + 1], "get_child", $i + 1);
                        if ($req3["data"]) {
                            $res["get_child" . ($i + 1)] = "";
                        } else {
                            $res["get_child" . ($i + 1)] = $req3;
                        }
                    }
                }
                $reqResult = $res;

                $row = 0;
                for ($i = $initLevel - 1; $i < ($initLevel - 1) + ceil(count($reqResult) / 2); $i++) {
                    $currentGeoData = $reqResult["get_current" . $i][0] ? $reqResult["get_current" . $i][0] : "null";
                    //$currentGeoData["level"] = GeoModel::getGeoIntLevel($currentGeoData["geo_code"]);
                    $levelType = GeoCodeUtil::getGeoLevel($currentGeoData["geo_code"]);
                    switch ($levelType) {
                        case "province" : $currentGeoData["GEOLEVEL"] = 1;
                            break;
                        case "city" : $currentGeoData["GEOLEVEL"] = 2;
                            break;
                        case "county" : $currentGeoData["GEOLEVEL"] = 3;
                            break;
                        case "town" : $currentGeoData["GEOLEVEL"] = 4;
                            break;
                        case "village" : $currentGeoData["GEOLEVEL"] = 5;
                            break;
                    }
                    $result[$row]["current"] = $currentGeoData;
                    $result[$row]["child"] = $reqResult["get_child" . $i];
                    $row++;
                }
            }
            TinMemcache::set($cacheKey, $result, 24 * 3600);
        }
        echo json_encode($result);
    }
    /** 根据上级ID获取下级地区列表 */
    public function actionGetGeoByParentId() {
        $parent_id = intval($_REQUEST["parent_id"]);
        $cacheKey = 'mall_GetGeoByParentId' . $parent_id;
        $data = TinMemcache::get($cacheKey);
        if (!$data) {
            $filter = "status > 1 AND PARENTID = " . $parent_id;
            $data = $this->geoLogic->getGeoAll("*", $filter);
            TinMemcache::set($cacheKey, $data, 24 * 3600);
        }
        echo json_encode($data);
    }
	/** 根据GEO_CODE获取单条地区信息 */
    public function actionGetGeoByCode() {
    	$code = intval($_REQUEST["code"]);
    	$cacheKey = "mall_GetGeoByCode" . $code;
    	$result = TinMemcache::get($cacheKey);
    	if (!$result) {
    		$result = $this->geoLogic->getGeoAll("*", 'GEO_CODE=' . $code);
    		TinMemcache::set($cacheKey, $result, 24 * 3600);
    	}
    	$levelType = GeoCodeUtil::getGeoLevel($result[0]['GEO_CODE']);
    	switch ($levelType) {
    		case "province" : $result[0]['GEOLEVEL'] = 1;
    		break;
    		case "city" : $result[0]['GEOLEVEL'] = 2;
    		break;
    		case "county" : $result[0]['GEOLEVEL'] = 3;
    		break;
    		case "town" : $result[0]['GEOLEVEL'] = 4;
    		break;
    		case "village" : $result[0]['GEOLEVEL'] = 5;
    		break;
    	}
    	echo json_encode($result);
    }
    /** 根据ID获取单条地区信息 */
    public function actionGetGeoById() {
        $id = intval($_REQUEST["id"]);
        $cacheKey = "mall_GetGeoById" . $id;
        $result = TinMemcache::get($cacheKey);
        if (!$result) {
            $result = $this->geoLogic->getGeoAll("*", 'id=' . $id);
            TinMemcache::set($cacheKey, $result, 24 * 3600);
        }
        $levelType = GeoCodeUtil::getGeoLevel($result[0]['GEO_CODE']);
        switch ($levelType) {
            case "province" : $result[0]['GEOLEVEL'] = 1;
                break;
            case "city" : $result[0]['GEOLEVEL'] = 2;
                break;
            case "county" : $result[0]['GEOLEVEL'] = 3;
                break;
            case "town" : $result[0]['GEOLEVEL'] = 4;
                break;
            case "village" : $result[0]['GEOLEVEL'] = 5;
                break;
        }
        echo json_encode($result);
    }

    private function buildGeoReq($geoCode, $type = "get_current", $num = "") {
    	$result = null;
        if ($type == "get_current") {
        	$result = $this->geoLogic->getGeoAll("*","geo_code = " . $geoCode);
        }
        if ($type == "get_child") {     	
        	$result = $this->geoLogic->getAllByParentGeo("*","status > 1 AND PARENT_GEO_CODE = " . $geoCode);
        }
        return $result;
    }

}

//End Class
?>
